<?php
/**
 * Registering meta boxes
 *
 * In this file, I'll show you how to extend the class to add more field type (in this case, the 'taxonomy' type)
 * All the definitions of meta boxes are listed below with comments, please read them carefully.
 * Note that each validation method of the Validation Class MUST return value instead of boolean as before
 *
 * You also should read the changelog to know what has been changed
 *
 * For more information, please visit: http://www.deluxeblogtips.com/2010/04/how-to-create-meta-box-wordpress-post.html
 *
 */

/********************* BEGIN EXTENDING CLASS ***********************/

/**
 * Extend RW_Meta_Box class
 * Add field type: 'taxonomy'
 */
class RW_Meta_Box_Taxonomy extends RW_Meta_Box {
	
	function add_missed_values() {
		parent::add_missed_values();
		
		// add 'multiple' option to taxonomy field with checkbox_list type
		foreach ($this->_meta_box['fields'] as $key => $field) {
			if ('taxonomy' == $field['type'] && 'checkbox_list' == $field['options']['type']) {
				$this->_meta_box['fields'][$key]['multiple'] = true;
			}
		}
	}
	
	// show taxonomy list
	function show_field_taxonomy($field, $meta) {
		global $post;
		
		if (!is_array($meta)) $meta = (array) $meta;
		
		$this->show_field_begin($field, $meta);
		
		$options = $field['options'];
		$terms = get_terms($options['taxonomy'], $options['args']);
		
		// checkbox_list
		if ('checkbox_list' == $options['type']) {
			foreach ($terms as $term) {
				echo "<input type='checkbox' name='{$field['id']}[]' value='$term->slug'" . checked(in_array($term->slug, $meta), true, false) . " /> $term->name<br/>";
			}
		}
		// select
		else {
			echo "<select name='{$field['id']}" . ($field['multiple'] ? "[]' multiple='multiple' style='height:auto'" : "'") . ">";
		
			foreach ($terms as $term) {
				echo "<option value='$term->slug'" . selected(in_array($term->slug, $meta), true, false) . ">$term->name</option>";
			}
			echo "</select>";
		}
		
		$this->show_field_end($field, $meta);
	}
}

/********************* END EXTENDING CLASS ***********************/

/********************* BEGIN DEFINITION OF META BOXES ***********************/

// prefix of meta keys, optional
// use underscore (_) at the beginning to make keys hidden, for example $prefix = '_rw_';
// you also can make prefix empty to disable it
$prefix = '';

$meta_boxes = array();


$front_page = array("No Front Page" => "No Front Page" );
$all_front_pages = get_front_pages();

if ( count($all_front_pages) > 0 )
{	
	foreach ( $all_front_pages as $single_front_page)
	{
		$front_page[$single_front_page->id] = $single_front_page->name;
	}
}

$display_featured_image_in_post = array(
							'yes' => 'Yes',
							'no'  => 'No',
							'video' => 'Video',
							'slideshow' => 'Slideshow'
						);

$meta_boxes[] = array(
	'id' => 'display_featured_image',
	'title' => 'Display Featured Image IN POST',
	'pages' => array('post'),

	'fields' => array(
		array(
			'name' => 'Display Featured Image when VIEWING post?',
			'id' => $prefix . 'display_featured_image',
			'type' => 'select',
			'options' => $display_featured_image_in_post,
			'desc' => ''
		)
	),
	'priority' => 'low',	
	'context' => 'side'
);

$meta_boxes[] = array(
	'id' => 'front_page',
	'title' => 'Front Page',
	'pages' => array('page'),

	'fields' => array(
		array(
			'name' => 'Select Front Page',
			'id' => $prefix . 'display_front_page',
			'type' => 'select',
			'options' => $front_page,
			'desc' => 'The Front Page you want to display on this Page'
		)
	),
	'priority' => 'low',	
	'context' => 'side'
);

/// THE SIDEBAR POSITION
$meta_boxes[] = array(
	'id' => 'sidebar_position',
	'title' => 'Sidebar Position',
	'pages' => array('page'),

	'fields' => array(
		array(
			'name' => 'Sidebar Position',
			'id' => $prefix . 'sidebar_position',
			'type' => 'select',
			'options' => array('website_selected' => 'Website Selected', 'left' => 'Left', 'right' =>'Right'),
			'desc' => 'Where do you want your sidebar'
		)//,
	),
	'priority' => 'low',	
	'context' => 'side'
);

/// POSTS PER PAGE
$posts_per_page = array(
							'-1' => 'All Posts',
							'1'  => '1',
							'2'  => '2',
							'3'  => '3',
							'4'  => '4',
							'5'  => '5',
							'6'  => '6',
							'7'  => '7',
							'8'  => '8',
							'9'  => '9',
							'10' => '10',
							'11' => '11',
							'12' => '12',
							'13' => '13',
							'14' => '14',
							'15' => '15',
							'16' => '16',
							'17' => '17',
							'18' => '18',
							'19' => '19',
							'20' => '20'
						);

$meta_boxes[] = array(
	'id' => 'posts_per_page',
	'title' => 'Posts Per Page',
	'pages' => array('page'),

	'fields' => array(
		array(
			'name' => 'Posts Per Page',
			'id' => $prefix . 'posts_per_page',
			'type' => 'select',
			'options' => $posts_per_page,
			'desc' => 'The number of posts per page you want to be displayed'
		)
	),
	'priority' => 'high',	
	'context' => 'normal'
);

/// CUSTOM SIDEBAR
global $wpdb;
$display_sidebars_query = 'SELECT * FROM `'.$wpdb->prefix.'lazar_sidebars` ORDER BY ID';
$display_sidebars = $wpdb->get_results($display_sidebars_query);

$custom_sidebars['no_custom_sidebar'] = 'No custom sidebar';

foreach ($display_sidebars as $display_sidebar)
	$custom_sidebars[$display_sidebar->id] = $display_sidebar->name;

$custom_sidebar_position = array(
							'custom_sidebar_only'          => 'Custom sidebar only',
							'custom_sidebar_before_before' => 'Before all sidebars',
							'custom_sidebar_after_before'  => 'After "Before all widgets" area',
							'custom_sidebar_after'         => 'After all sidebars'
						);

$meta_boxes[] = array(
	'id' => 'custom_sidebars',
	'title' => 'Custom Sidebars',
	'pages' => array('page','post','portfolios'),

	'fields' => array(
		array(
			'name' => 'Custom Sidebars',
			'id' => $prefix . 'custom_sidebars',
			'type' => 'select',
			'options' => $custom_sidebars,
			'desc' => 'Use this if you wish to modify the default sidebar for this certain post'
		),
		array(
			'name' => 'Custom Sidebar Mode',
			'id' => $prefix . 'custom_sidebar_position',
			'type' => 'select',
			
			'options' => $custom_sidebar_position,
			'desc' => 'Use this to select where you wish your custom sidebar to be displayed'
		)
		
//////////	
	),
	'priority' => 'high',	
	'context' => 'normal'
);

// VIDEO LINK
$meta_boxes[] = array(
	'id' => 'featured_video_link',
	'title' => 'Featured Video Link ( embed )',
	'pages' => array('post'),

	'fields' => array(
		array(
			'name' => 'Featured Video Link ( embed )',
			'id' => $prefix . 'featured_video_link',
			'type' => 'text',
			'desc' => 'Copy your embeded code here and go to display featured image in post (right column) and select VIDEO.<br />We recommend 630px width when viewing with sidebar and 960px when viewing full width.'
		)
	),
	'priority' => 'high',	
	'context' => 'normal'
);

// SLIDESHOW HEIGHT
$meta_boxes[] = array(
	'id' => 'single_slideshow_height',
	'title' => 'Single Slideshow Height',
	'pages' => array('post'),

	'fields' => array(
		array(
			'name' => 'Single Slideshow Height',
			'id' => $prefix . 'single_slideshow_height',
			'type' => 'text',
			'desc' => 'The Post Slideshow Height.'
		)
	),
	'priority' => 'high',	
	'context' => 'normal'
);

foreach ($meta_boxes as $meta_box) {
	$my_box = new RW_Meta_Box_Taxonomy($meta_box);
}

/********************* END DEFINITION OF META BOXES ***********************/

?>
